package com.tiny.work.test.threadpool;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPoolOutTest3 {
    private static ReentrantLock A = new ReentrantLock();
    private static         int capacity = 10;
    private static int count = 0;


    public static void main(String args[]) throws InterruptedException, ExecutionException {
        Thread t1 = new Thread(new RunnableTaskA('A'));
        Thread t2 = new Thread(new RunnableTaskB('B'));
        Thread t3 = new Thread(new RunnableTaskC('C'));
        t1.start();
        t2.start();
        t3.start();
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        Collection<Callable<Void>> tasks  = new ArrayList<>();
        tasks.add(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                return null;
            }
        });
        for (Future future : executorService.invokeAll(tasks)) {
            future.get();
        }

    }

    public static class RunnableTaskA implements Runnable{

        char data;

        @Override
        public void run() {
            for (int i=0; i< capacity; ) {
                A.lock();

                if (count % 3 == 0) {
                    System.out.println(data);
                    i++;
                    count++;

                }
                    A.unlock();
            }
        }

        public RunnableTaskA(char data) {
            this.data= data;
        }
    }
    public static class RunnableTaskB  implements Runnable{

        char data;

        @Override
        public void run() {
            for (int i=0; i< capacity;) {
                A.lock();
                if (count % 3 == 1) {
                    System.out.println(data);
                    i++;
                    count++;
                }
                    A.unlock();
            }
        }

        public RunnableTaskB(char data) {
            this.data= data;
        }
    }
    public static class RunnableTaskC implements Runnable{

        char data;

        @Override
        public void run() {
            for (int i=0; i< capacity; ) {
                A.lock();
                if (count % 3 == 2){
                    System.out.println(data);
                    i++;
                    count++;

                }
                    A.unlock();
            }
        }

        public RunnableTaskC(char data) {
            this.data= data;
        }
    }
}
